package com.hyt.it.ogt.ykcj.service.grade.impl;

import com.hyt.it.ogt.ykcj.common.utils.DateUtils;
import com.hyt.it.ogt.ykcj.domain.grade.EnterpriseExamineeSubjectGrade;
import com.hyt.it.ogt.ykcj.domain.vo.EnterpriseExamineeSubjectGradeVo;
import com.hyt.it.ogt.ykcj.domain.vo.ExamScoreVo;
import com.hyt.it.ogt.ykcj.mapper.grade.EnterpriseExamineeSubjectGradeMapper;
import com.hyt.it.ogt.ykcj.service.grade.IEnterpriseExamineeSubjectGradeService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 企业招聘考生科目成绩Service业务层处理
 * 
 * @author liying
 * @date 2021-11-19
 */
@Service
public class EnterpriseExamineeSubjectGradeServiceImpl implements IEnterpriseExamineeSubjectGradeService
{
    @Autowired
    private EnterpriseExamineeSubjectGradeMapper enterpriseExamineeSubjectGradeMapper;

    /**
     * 查询企业招聘考生科目成绩
     * 
     * @param id 企业招聘考生科目成绩ID
     * @return 企业招聘考生科目成绩
     */
    @Override
    public EnterpriseExamineeSubjectGrade selectServantExamineeSubjectGradeById(String id)
    {
        return enterpriseExamineeSubjectGradeMapper.selectEnterpriseExamineeSubjectGradeById(id);
    }

    /**
     * 查询企业招聘考生科目成绩列表
     * 
     * @param enterpriseExamineeSubjectGrade 企业招聘考生科目成绩
     * @return 企业招聘考生科目成绩
     */
    @Override
    public List<EnterpriseExamineeSubjectGrade> selectEnterpriseExamineeSubjectGradeList(EnterpriseExamineeSubjectGrade enterpriseExamineeSubjectGrade)
    {
        return enterpriseExamineeSubjectGradeMapper.selectEnterpriseExamineeSubjectGradeList(enterpriseExamineeSubjectGrade);
    }

    /**
     * 新增企业招聘考生科目成绩
     * 
     * @param enterpriseExamineeSubjectGrade 新增企业招聘考生科目成绩
     * @return 结果
     */
    @Override
    public int insertEnterpriseExamineeSubjectGrade(EnterpriseExamineeSubjectGrade enterpriseExamineeSubjectGrade)
    {
        enterpriseExamineeSubjectGrade.setCreateTime(DateUtils.getNowDate());
        return enterpriseExamineeSubjectGradeMapper.insertEnterpriseExamineeSubjectGrade(enterpriseExamineeSubjectGrade);
    }

    /**
     * 修改企业招聘考生科目成绩
     * 
     * @param enterpriseExamineeSubjectGrade 修改企业招聘考生科目成绩
     * @return 结果
     */
    @Override
    public int updateEnterpriseExamineeSubjectGrade(EnterpriseExamineeSubjectGrade enterpriseExamineeSubjectGrade)
    {
        enterpriseExamineeSubjectGrade.setUpdateTime(DateUtils.getNowDate());
        return enterpriseExamineeSubjectGradeMapper.updateEnterpriseExamineeSubjectGrade(enterpriseExamineeSubjectGrade);
    }

    /**
     * 批量删除企业招聘考生科目成绩
     * 
     * @param ids 需要删除的企业招聘考生科目成绩ID
     * @return 结果
     */
    @Override
    public int deleteEnterpriseExamineeSubjectGradeByIds(String[] ids)
    {
        return enterpriseExamineeSubjectGradeMapper.deleteEnterpriseExamineeSubjectGradeByIds(ids);
    }

    /**
     * 删除企业招聘考生科目成绩信息
     *
     * @param id 企业招聘考生科目成绩ID
     * @return 结果
     */
    @Override
    public int deleteEnterpriseExamineeSubjectGradeById(String id)
    {
        return enterpriseExamineeSubjectGradeMapper.deleteEnterpriseExamineeSubjectGradeById(id);
    }

    /**
     * 获取考试成绩总份数
     * @param examIds
     * @return
     */
    @Override
    public Map<String,Integer> getExamineeSubjectGradeCountByExamIds(List<String> examIds){
        if(CollectionUtils.isEmpty(examIds)){
            return Collections.EMPTY_MAP;
        }
        List<EnterpriseExamineeSubjectGradeVo> gradeVos = enterpriseExamineeSubjectGradeMapper.selectExamineeSubjectGradeByExamIds(examIds.toArray(new String[examIds.size()]));
        if(CollectionUtils.isEmpty(gradeVos)){
            return Collections.EMPTY_MAP;
        }
        Map<String, List<EnterpriseExamineeSubjectGradeVo>> gradeMap = gradeVos.stream().collect(Collectors.groupingBy(c -> c.getExamId()));
        Map countMap = new HashMap(gradeMap.size());
        for (Map.Entry<String, List<EnterpriseExamineeSubjectGradeVo>> entry : gradeMap.entrySet()) {
            countMap.put(entry.getKey(),CollectionUtils.isEmpty(entry.getValue()) ? 0 : entry.getValue().size());
        }
        return  countMap;
    }


    @Override
    public  List<ExamScoreVo> getExamineeMaxScore(List<String> examIds ) {
        return enterpriseExamineeSubjectGradeMapper.getExamineeMaxScore(examIds.toArray(new String[examIds.size()]));
    }

    @Override
    public  List<ExamScoreVo> getExamineeMinScore(List<String> examIds) {
        return enterpriseExamineeSubjectGradeMapper.getExamineeMinScore(examIds.toArray(new String[examIds.size()]));
    }
}
